using System.Globalization;

namespace RadioTimer.Converters
{
    /// <summary>
    /// 颜色亮化转换器
    /// 将输入颜色进行亮化处理，用于边框等需要高亮效果的场景
    /// </summary>
    public class ColorLightenConverter : IValueConverter
    {
        /// <summary>
        /// 将颜色值转换为亮化后的颜色
        /// </summary>
        /// <param name="value">输入的颜色值</param>
        /// <param name="targetType">目标类型</param>
        /// <param name="parameter">转换参数（亮化因子，默认0.2）</param>
        /// <param name="culture">文化信息</param>
        /// <returns>亮化后的颜色</returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is Color color)
            {
                // 获取亮化因子，默认0.2
                float factor = 0.2f;
                if (parameter is string paramString && float.TryParse(paramString, out float parsedFactor))
                {
                    factor = parsedFactor;
                }

                // 计算亮化后的颜色
                var lighterColor = LightenColor(color, factor);
                return lighterColor;
            }

            // 如果无法转换，返回默认的白色
            return Colors.White;
        }

        /// <summary>
        /// 反向转换（不支持）
        /// </summary>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 亮化颜色
        /// </summary>
        /// <param name="color">原始颜色</param>
        /// <param name="factor">亮化因子</param>
        /// <returns>亮化后的颜色</returns>
        private Color LightenColor(Color color, float factor)
        {
            var r = (int)Math.Min(255, color.Red * 255 + (255 - color.Red * 255) * factor);
            var g = (int)Math.Min(255, color.Green * 255 + (255 - color.Green * 255) * factor);
            var b = (int)Math.Min(255, color.Blue * 255 + (255 - color.Blue * 255) * factor);
            return Color.FromRgb((byte)r, (byte)g, (byte)b);
        }
    }
}